package com.hainingtong.core.shiro.realm;

import com.hainingtong.core.config.JwtProperties;
import com.hainingtong.core.entity.SysUser;
import com.hainingtong.core.service.UserService;
import com.hainingtong.core.shiro.token.CustomUsernamePasswordToken;
import com.hainingtong.core.shiro.token.JwtToken;
import com.hainingtong.core.util.JwtUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

public class JwtRealm extends AuthorizingRealm {
    @Autowired
    JwtProperties jwtProperties;

    @Autowired
    UserService userService;

    /**
     * 自定义匹配密码的方法
     */
    public JwtRealm() {
        this.setCredentialsMatcher(new CredentialsMatcher() {
            @Override
            public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) {
                return JwtUtil.verify(authenticationToken.getPrincipal().toString(), jwtProperties.getSecretKey());
            }
        });
    }


    /**
     * 只处理对应类型的认证
     *
     * @param token
     * @return 如果是对应的类型就返回true，表示会执行doGetAuthorizationInfo获取用户信息用于认证；false就跳过当前realm
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取登录用户名
        String token = (String) principalCollection.getPrimaryPrincipal();

        // 登录账号
        String principal = JwtUtil.getClaim(token,"principal");
        // 登录类型
        String type = JwtUtil.getClaim(token,"type");

        // 去数据库中获取数据
        SysUser user = userService.findUserByPrincipalAndType(principal,type);


        // 获取用户对应的角色和权限信息，然后添加进去

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addRole("admin");
//        for (Role role : user.getRoles()) {
//            //添加角色
//            simpleAuthorizationInfo.addRole(role.getRoleName());
//            //添加权限
//            for (Permissions permissions : role.getPermissions()) {
//                simpleAuthorizationInfo.addStringPermission(permissions.getPermissionsName());
//            }
//        }
        return simpleAuthorizationInfo;
    }

    /**
     * @Description 认证配置类
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        if (!StringUtils.hasText(authenticationToken.getPrincipal().toString())) {
            return null;
        }

        // jwt 是直接验证 token数据，如果token合法就表示已登录，所以无需从数据库获取账号密码来比较

        return new SimpleAuthenticationInfo(authenticationToken.getPrincipal(), authenticationToken.getPrincipal().toString(), getName());
    }
}
